package com.example.validdemo.validator;

import com.example.validdemo.model.SysUserAgeVO;
import org.hibernate.validator.spi.group.DefaultGroupSequenceProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Bx_Hu
 */
public class AgeGroupSequenceProvider
        implements DefaultGroupSequenceProvider<SysUserAgeVO> {

    @Override
    public List<Class<?>> getValidationGroups(SysUserAgeVO sysUserVO) {
        int age = 60;
        List<Class<?>> defaultGroupSequence = new ArrayList<Class<?>>();
        //加入默认校验组
        defaultGroupSequence.add(SysUserAgeVO.class);
        if (sysUserVO == null || sysUserVO.getAge() == null) {
            return defaultGroupSequence;
        }

        //根据年龄加入对应的校验组
        //入参年龄大于60岁,添加禁用的组,否则添加正常的组
        if (sysUserVO.getAge() > age) {
            defaultGroupSequence.add(SysUserAgeVO.Forbidden.class);
        } else {
            defaultGroupSequence.add(SysUserAgeVO.Normal.class);
        }
        return defaultGroupSequence;
    }
}
